package com.seguriapp.dialogs;

import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.seguriapp.R;
import com.seguriapp.adapters.SelectCategoryListAdapter;
import com.seguriapp.controllers.ComplaintController;
import com.seguriapp.controllers.EntityController;
import com.seguriapp.dao.EntityCategoryDao;
import com.seguriapp.other.CategoryDialogInterface;

/**
 * TODO(pjohnson): Insert description
 * 
 * @author Pablo Johnson (pablo.88j@gmail.com)
 */
public class SelectCategoryDialog extends DialogFragment {

	private CategoryDialogInterface mInterface;
	private int mFlag;

	public final static int CRIME_CATEGORY = 0;
	public final static int ENTITY_CATEGORY = 1;

	public SelectCategoryDialog(final CategoryDialogInterface theInterface,
			final int flag) {
		mInterface = theInterface;
		mFlag = flag;
	}

	@Override
	public void onCreate(Bundle arg0) {
		setStyle(DialogFragment.STYLE_NO_FRAME,
				android.R.style.Theme_Holo_Dialog);
		super.onCreate(arg0);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		final View view = inflater.inflate(R.layout.dialog_crime, container);

		final ListView lstVCategorias = (ListView) view
				.findViewById(R.id.lstCrimenes);
		final int[] arrayTo = new int[] { R.id.nombreCategoria,
				R.id.seleccionCategoria };
		final String[] arrayFrom = new String[] { EntityCategoryDao.COL_NAME,
				EntityCategoryDao.COL_SELECTED };

		Cursor cursor;
		if (mFlag == CRIME_CATEGORY) {
			getDialog().setTitle(
					getDialog().getContext().getString(
							R.string.msgCrimeCategory));
			final ComplaintController cController = new ComplaintController(
					getDialog().getContext());
			cursor = cController.getComplaintCategoriesForDialog();

		} else {
			getDialog().setTitle(
					getDialog().getContext().getString(
							R.string.msgEntityCategory));
			final EntityController eController = new EntityController(
					getDialog().getContext());
			cursor = eController.getEntityCategories();
		}
		final SelectCategoryListAdapter lstAdapter = new SelectCategoryListAdapter(
				getDialog().getContext(), R.layout.list_select_category,
				cursor, arrayFrom, arrayTo, mFlag);
		lstVCategorias.setAdapter(lstAdapter);

		return view;
	}

	@Override
	public void onDismiss(DialogInterface arg0) {
		mInterface.onChangeCategories();
		super.onDismiss(arg0);
	}

}
